using System;
using Microsoft.SPOT;

namespace Gadgeteer.Interfaces.Sample.Safe.SafeApp
{
    public class LockBase : ILock
    {
        #region events

        public event LockStateChange Opened;

        private void OnOpened()
        {
            var ev = Opened;
            if (ev != null)
                ev(this, new LockStateEventArgs(true));
        }

        public event LockStateChange Closed;

        private void OnClosed()
        {
            var ev = Closed;
            if (ev != null)
                ev(this, new LockStateEventArgs(false));
        }
        #endregion

        public void Close()
        {
            IsOpen = false;
        }

        private bool open = false;
        public bool IsOpen
        {
            get { return open; }
            protected set
            {
                if (open != value)
                {
                    open = value;
                    if (open)
                        OnOpened();
                    else
                        OnClosed();
                }
            }
        }
    }
}
